package com.oauth.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

/**
 * @author liwenjian
 * @date 2021/7/12 23:02
 *  权限控制设置 https://www.cnblogs.com/zhncnblogs/p/16314024.html
 *  添加@EnableGlobalMethodSecurity(prePostEnabled = true)
 *  1，@PreAuthorize("#oauth2.hasScope('fly')") 客户端权限范围控制
 *  2，@PreAuthorize("hasRole('ROLE_USER')")    用户权限范围控制
 *
 * 资源服务器配置
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        //资源id，要和授权服务器给客户端配置的一致
        resources.resourceId("resourceId");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .requestMatchers()
                .antMatchers("/**","/user/**","/test/**");
//        http.authorizeRequests().anyRequest().permitAll();
    }

}
